package com.nxw.springcloud.admin.service.impl;

import com.nxw.springcloud.admin.entity.AppKeyAndSecret;
import com.nxw.springcloud.admin.enums.EnvironmentEnum;
import com.nxw.springcloud.admin.respository.AppKeyAndSecretDao;
import com.nxw.springcloud.admin.service.common.AppUtils;
import com.nxw.springcloud.admin.service.inf.AppKeyAndSecretService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @program: dynamic-route
 * @description:
 * @author: nxw
 * @create: 2020-02-25 10:06
 **/
@Service
public class AppKeyAndSecretImpl implements AppKeyAndSecretService {

    @Autowired
    AppKeyAndSecretDao dao;

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public AppKeyAndSecret getAppKeyAndSecret(EnvironmentEnum environment) {
        String appId = AppUtils.getAppId();
        String appSecret = AppUtils.getAppSecret(appId);
        AppKeyAndSecret appKeyAndSecret = AppKeyAndSecret.builder().appKey(appId).appSecret(appSecret).environment(environment).build();
        AppKeyAndSecret save = dao.save(appKeyAndSecret);
        redisTemplate.opsForHash().put(environment.getValue(),save.getAppKey(),save.getAppSecret());
        return save;
    }

    @Override
    public AppKeyAndSecret resetAppKeyAndSecret(String appKey, EnvironmentEnum environment) {
        AppKeyAndSecret appKeyAndSecretByAppKey = dao.findAppKeyAndSecretByAppKeyAndEnvironment(appKey,environment);

        if (appKeyAndSecretByAppKey == null){
            appKeyAndSecretByAppKey = new AppKeyAndSecret();
            String appId = AppUtils.getAppId();
            String appSecret = AppUtils.getAppSecret(appId);
            appKeyAndSecretByAppKey.setAppKey(appId);
            appKeyAndSecretByAppKey.setAppSecret(appSecret);
            appKeyAndSecretByAppKey.setEnvironment(environment);
        }else {
            String appKeyId = appKeyAndSecretByAppKey.getAppKey();
            appKeyAndSecretByAppKey.setAppKey(appKeyId);
            appKeyAndSecretByAppKey.setAppSecret(AppUtils.getAppSecret(appKeyId));
            appKeyAndSecretByAppKey.setEnvironment(environment);
        }
        AppKeyAndSecret save = dao.save(appKeyAndSecretByAppKey);
        if (!StringUtils.isEmpty(redisTemplate.opsForHash().get(environment.getValue(),appKey))){
            redisTemplate.opsForHash().delete(environment.getValue(),appKey);
        }
        redisTemplate.opsForHash().put(environment.getValue(),save.getAppKey(),save.getAppSecret());
        return save;
    }

    @Override
    public AppKeyAndSecret getAppkeyByAppKey(String appKey, EnvironmentEnum environment) {
        AppKeyAndSecret appKeyAndSecretByAppKey = dao.findAppKeyAndSecretByAppKeyAndEnvironment(appKey,environment);
        if (appKeyAndSecretByAppKey != null){
            redisTemplate.opsForHash().put(environment.getValue(),appKeyAndSecretByAppKey.getAppKey(),appKeyAndSecretByAppKey.getAppSecret());
        }
        return  appKeyAndSecretByAppKey;
    }
}
